package com.wanxing.main;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.zhny.wxginger.R;
import com.util.HttpUtils;
import com.util.Info;

@SuppressLint("NewApi")
public class ControlFragment extends Fragment {
//	PickerView bfpoint1;
//	PickerView bfpoint2;
//	PickerView bfpoint3;
//	PickerView wateringYear, wateringMonth, wateringDay, wateringHour,
//			wateringMinute;
	String maxhumi;
	String minhumi;
	String ntime;
	String nyear;
	String nmonth;
	String nday;
	String nhour;
	String nmintue;
	private String DTU = null;
	private Handler handler;
	private ArrayAdapter<String> lab_adapter;
	boolean flag = false;
	Boolean s_switch = false;

	Button b2;// ����
	Button b3;// ����

	Switch myswitch;

	// @Override
	// protected void onCreate(Bundle savedInstanceState) {
	// super.onCreate(savedInstanceState);
	// setContentView(R.layout.control_set);
	// System.out.println("�л��ɹ�");
	// viewFind();
	// viewSet();
	// ActionBar actionBar = getActionBar();
	// actionBar.setDisplayShowTitleEnabled(false);
	// actionBar.setDisplayShowHomeEnabled(false);
	// }

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.control_set, container, false);
		viewFind(view);

		viewSet();
		String labname = getActivity().getActionBar().getTitle().toString();
		DTU = labname;
		//new myAsyncTask().execute(Info.path8 + DTU);// ��ѯ��ʷ
		new myAsyncTask2().execute(Info.path2 + DTU);
		return view;
	}

	public void viewFind(View view) {
		myswitch = (Switch) view.findViewById(R.id.mySwitch);
		b2 = (Button) view.findViewById(R.id.Button2);
		b3 = (Button) view.findViewById(R.id.Button3);
	}

	private void showDialog(String str) {
		AlertDialog.Builder builder;
		builder = new AlertDialog.Builder(this.getActivity());
		builder.setTitle("��ʾ��");
		builder.setMessage(str);
		builder.setPositiveButton("ȷ��", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
			}
		});
		AlertDialog dialog = builder.create();
		dialog.show();
	}


	private void showDialog1(final String split) {
		LayoutInflater inflater = LayoutInflater.from(this.getActivity());
		final View view = inflater.inflate(R.layout.dialog_item, null);
		AlertDialog.Builder builder;
		builder = new AlertDialog.Builder(this.getActivity());
		builder.setTitle("���������������֤");
		builder.setView(view);
		builder.setPositiveButton("�ύ", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				EditText id = (EditText) view.findViewById(R.id.ed_id);
				String sid = id.getText().toString();
				if (sid.equals("112233")) {
					// ����ָ��
					if (split.equals("open")) {
						// if(DTU.equals("001")){
						// String command="BB00dezhoulx0001aa";
						// new Thread(new Sendcommand(handler,
						// command)).start();
						// }else if(DTU.equals("002")){
						// String command="BB00dezhoulx0002aa";
						// new Thread(new Sendcommand(handler,
						// command)).start();
						// }else if(DTU.equals("006")){
						// String command="BB00dezhoulx0002aa";
						// new Thread(new Sendcommand(handler,
						// command)).start();
						// }
						String command = "BB00dezhoulx" + DTU + "AE";
						s_switch = true;

						new myAsyncTask2().execute(Info.path2 + DTU);
					} else if (split.equals("close")) {
						// if(DTU.equals("001")){
						// String command="BB00dezhoulx0001ab";
						// new Thread(new Sendcommand(handler,
						// command)).start();
						// }else if(DTU.equals("002")){
						// String command="BB00dezhoulx0002ab";
						// new Thread(new Sendcommand(handler,
						// command)).start();
						// }else if(DTU.equals("006")){
						// String command="BB00dezhoulx0002ab";s
						// new Thread(new Sendcommand(handler,
						// command)).start();
						// }
						String command = "BB00dezhoulx" + DTU + "AF";
						s_switch = false;

						new myAsyncTask2().execute(Info.path2 + DTU);
					} 
					s_switch = myswitch.isChecked();
				} else {
					if (split.equals("open")) {
						myswitch.setChecked(s_switch);
						System.out.println("��֤ʧ��" + "open");
					} else if (split.equals("close")) {
						myswitch.setChecked(s_switch);
						System.out.println("��֤ʧ��" + "close");
					}
					Toast.makeText(getActivity(), "��֤ʧ��", 1).show();
				}
				Toast.makeText(getActivity(), "�ù�����ʱ�޷�ʹ��", 1).show();
			}
		});
		builder.setNegativeButton("ȡ��", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				if (split.equals("open")) {
					myswitch.setChecked(s_switch);
					System.out.println("ȡ��" + "open");
				} else if (split.equals("close")) {
					myswitch.setChecked(s_switch);
					System.out.println("ȡ��" + "close");
				}
			}
		});
		AlertDialog dialog = builder.create();
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();

	}

	public void viewSet() {
		
		myswitch.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (myswitch.isChecked())
					showDialog1("open");
				else if (!myswitch.isChecked())
					showDialog1("close");
				System.out.println("myswitch�ĵ���¼�");
				

			}
		});
		b2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// flag = false;
				Intent intent = new Intent(getActivity(), Dialog_water_Activity.class);
				
				intent.putExtra("dtu", DTU);
				startActivity(intent);

			}
		});
		
		b3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//showDialog3();
				Intent intent = new Intent(getActivity(), Dialog_time_Activity.class);
				startActivity(intent);
			}
		});
	}


	class myAsyncTask2 extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			String result = HttpUtils.sendPostMethod(params[0], "utf-8");
			System.out.println(result+"--------get ˮ�õ��");
			return result;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);

			if (result == null) {
				myswitch.setChecked(false);
				s_switch = false;
			} else if (result.equals("10.00")) {
				myswitch.setChecked(true);
				s_switch = true;
			} else if (result.equals("00.00")) {
				myswitch.setChecked(false);
				s_switch = false;
			}
		}

	}

}
